package com.sailors.common.task.provider.domian.factories;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sailors.common.task.provider.domian.convert.FileTaskEntityConver;
import com.sailors.common.task.provider.domian.model.entity.FileTaskEntity;
import com.sailors.common.task.provider.domian.model.identity.TaskIdentity;
import com.sailors.common.task.provider.faced.model.dto.execute.FileTaskCreateExecuteDto;
import com.sailors.common.task.provider.faced.model.enums.TaskAsyncEnum;
import com.sailors.common.task.provider.faced.model.vo.TaskProLogVo;
import com.sailors.common.task.provider.infra.design.common.exception.BussinessException;
import com.sailors.common.task.provider.infra.design.common.util.ObjectUtil;
import com.sailors.common.task.provider.infra.repository.IFileTaskLogRepository;
import com.sailors.common.task.provider.infra.repository.ITaskProLogRepository;
import com.sailors.common.task.provider.infra.repository.factories.FileTaskRepositoryFactory;
import com.sailors.common.task.provider.infra.repository.model.FileTaskLogPo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * File task entity factory
 *
 * @description:
 * @author: cyj
 * @date 2022 -11-23 11:41:52
 */
public class FileTaskEntityFactory {

    /**
     * 获取新领域对象
     *
     * @param fileTaskCreateExecuteDto file task create execute dto
     * @return the file task entity
     */
    public static FileTaskEntity obtain(FileTaskCreateExecuteDto fileTaskCreateExecuteDto) {
        FileTaskEntity fileTaskEntity = ObjectUtil.obtain(FileTaskEntity.class);
        fileTaskEntity.setCommonProperty(FileTaskEntityConver.INSTANCE.dto2Property(fileTaskCreateExecuteDto));
        // 如果是异步步的不操作状态
        if (TaskAsyncEnum.YES.getCode().equals(fileTaskEntity.getCommonProperty().getAsync())) {
            fileTaskEntity.getCommonProperty().setStatus(null);
        }
        fileTaskEntity.setTaskProValueObjs(new ArrayList<>());
        // 设置标识
        fileTaskEntity.setIdentify(new TaskIdentity(IdWorker.getId()));
        // 设置仓储对象
        wrapperRepository(fileTaskEntity);
        return fileTaskEntity;
    }

    /**
     * 获取领域对象
     *
     * @param taskId
     * @return the file task entity
     */
    public static FileTaskEntity obtain(Long taskId) {
        FileTaskEntity fileTaskEntity = ObjectUtil.obtain(FileTaskEntity.class);
        FileTaskLogPo fileTaskLogPo = Optional.ofNullable(FileTaskRepositoryFactory.getRepository(IFileTaskLogRepository.class)
                .getById(taskId)).orElseThrow(() -> new BussinessException("任务不存在!"));
        // 设置通用属性
        fileTaskEntity.setCommonProperty(FileTaskEntityConver.INSTANCE.po2Property(fileTaskLogPo));
        // 设置标识
        fileTaskEntity.setIdentify(new TaskIdentity(fileTaskLogPo.getId()));

        List<TaskProLogVo> taskProLogvVos = FileTaskRepositoryFactory.getRepository(ITaskProLogRepository.class).getByTaskIds(Arrays.asList(fileTaskLogPo.getId()));
        // 设置进度
        fileTaskEntity.setTaskProValueObjs(FileTaskEntityConver.INSTANCE.proVos2Vals(taskProLogvVos));
        // 设置仓储对象
        wrapperRepository(fileTaskEntity);
        return fileTaskEntity;
    }

    private static void wrapperRepository(FileTaskEntity fileTaskEntity) {
        if (null == fileTaskEntity) {
            return;
        }
        fileTaskEntity.setTaskLogRepository(FileTaskRepositoryFactory.getRepository(IFileTaskLogRepository.class));
        fileTaskEntity.setTaskProLogRepository(FileTaskRepositoryFactory.getRepository(ITaskProLogRepository.class));
    }
}
